package com.wei.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wei.entity.Teacher;
import com.wei.entity.vo.TeacherQuery;
import com.wei.service.TeacherService;
import com.wei.service_base.exception.XueYuanException;
import com.wei.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/eduservice/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;


    @GetMapping("/getAllTeacher")
    public R findall() {
        List<Teacher> list = teacherService.list();
        return R.ok().data("teacherList", list);
    }

    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageteacher(@PathVariable Integer current, @PathVariable Integer limit) {
        Page<Teacher> teacherPage = new Page<>(current, limit);
        teacherService.page(teacherPage, null);
        long total = teacherPage.getTotal();
        List<Teacher> list = teacherPage.getRecords();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", list);
        return R.ok().data(map);
    }

    @DeleteMapping("{id}")
    public R deletebyid(@PathVariable Integer id) {
        boolean b = teacherService.removeById(id);
        return b ? R.ok() : R.error();
    }

    @PostMapping("getPageTeacher/{current}/{limit}")
    public R teacherCondition(@PathVariable Integer current, @PathVariable Integer limit, @RequestBody(required = false) TeacherQuery teacherQuery) {
        Page<Teacher> teacherPage = teacherService.teacherCondition(current, limit, teacherQuery);
        long total = teacherPage.getTotal();
        List<Teacher> list = teacherPage.getRecords();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("records", list);
        return R.ok().data(map);
    }

    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody Teacher teacher) {
        boolean save = teacherService.save(teacher);
        return save ? R.ok() : R.error();
    }

    @GetMapping("getTeacherById/{id}")
    public R getbyid(@PathVariable Integer id) {
        Teacher teacher = teacherService.getById(id);
        return teacher != null ? R.ok().data("teacher", teacher) : R.error().message("老师不存在");
    }

    @PutMapping("putTeacherById")
    public R putbyid(@RequestBody Teacher teacher) {
        boolean b = teacherService.updateById(teacher);
        return b ? R.ok() : R.error();
    }
}
